

/**
 * v1 一个大任务
 * @return {[type]} [description]
 */
 function v1() {
 	console.time("runtime");
 	function hd(num) {
 		let sum = 0, count = 0;
 		for (var i = 0; i <= num; i++) {
 			sum += i;
 			count++
 		}
 		console.log(`sum = ${sum}, i = ${i} count= ${count}`);
 		console.timeEnd("runtime");
 	}
 	let num=187654321;
 	hd(num);
	console.log(arguments.callee.name + " houdunren.com " ); //需要等待上面执行完才会执行

}

/**
 * js单线程: 必须 当前的index.js脚本执行完成后， 后面的 script脚本才能执行
 * @return {[type]} [description]
 */
 function v2() {
 	console.time("runtime");
 	function hd(num) {
 		let sum = 0, count = 0;
 		for (var i = 0; i <= num; i++) {
 			sum += i;
 		}
 		console.log(`sum = ${sum}, i = ${i} count= ${count}`);
 		console.timeEnd("runtime");
 	}
 	let num=187654321;

 	setTimeout(() => {
 		hd(num);
 	})


	console.log("houdunren.com setTimeout"); //需要等待上面执行完才会执行
	console.log(arguments.callee.name + ' 不需要影响后面的执行')
	
}


/**
 * 将大任务拆分为单个的小任务 
 * @return {[type]} [description]
 */
 function v3() {
 	console.time("runtime");
 	let sum = 0, count = 0;
 	function hd() {
 		for (var  i = 0; i < 10000000; i++) {
 			if (num <= 0) break;
 			sum += num;
 			num --
 			count++;
 		}

 		if(num > 0) {
 			console.log(`num = ${num}`)
 			setTimeout(hd)
 		} else {
			// 执行完成了 num =  0
			// sum = 48841639102092800, i = 87654320
			// runtime: 34989.15869140625 ms

 			console.log('执行完成了 num = ', num)
 			console.log(`sum = ${sum}, i = ${i} count= ${count}`);
 			console.timeEnd("runtime");
 		}

 	}

 	let num=187654321;
 	hd(num);
	console.log("houdunren.com  多个小的宏任务"); //需要等待上面执行完才会执行
	console.log(arguments.callee.name + ' 不需要影响后面的执行')
	
}

v4()



/**
 * Promise then 微任务
 * @return {[type]} [description]
 */
function v4() {
console.time("runtime");
 	let sum = 0, count = 0;
 	function hd() {
 		Promise.resolve().then(() => {
 			for (var  i = 0; i <= num; i++) {
	 			sum += i
	 			count++
	 		}
			// sum = 487730528962094400, i = 987654322, count= 987654322
			//  runtime: 35947.221923828125 ms
 			console.log(`sum = ${sum}, i = ${i}, count= ${count}`);
 			console.timeEnd("runtime");
 		})
 	}

 	let num=187654321;
 	hd(num);
	console.log("houdunren.com Promise then 微任务"); //需要等待上面执行完才会执行
	console.log(arguments.callee.name + ' 不需要影响后面的执行')

}







/*

console.time("runtime");
let sum = 0, count = 0;
let num = 98765432;
function hd() {
	for (var i = 0; i < 10000000; i++) {
		if (num <= 0) break;
		sum += num--;
	}
	if (num > 0) {
		console.log(num);
		setTimeout(hd);
	} else {
		console.log(num);
		console.log(sum);
		console.timeEnd("runtime");

	}
}
hd();
console.log("houdunren.com"); //立刻显示出来



async function hd(num) {
	let res = await Promise.resolve().then(_ => {
		let sum = 0, count = 0;
		for (var i = 0;  num >=0; i++) {
			sum += num--;
		}
		console.log('sum = ' + sum)
		
		return sum;
	});
	console.log(res);
	console.timeEnd("后盾人");

}

console.time("后盾人");

hd(98765432);
console.log("后盾人");





*/